package com.chery.media.helper

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

object BluetoothHelper {

    // 检查蓝牙权限
    fun checkBluetoothPermissions(context: Context): Boolean {
        // 检查蓝牙权限
        val hasBluetoothPermission = ContextCompat.checkSelfPermission(
            context, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED

        // 检查蓝牙管理权限
        val hasBluetoothAdminPermission = ContextCompat.checkSelfPermission(
            context, Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_GRANTED

        // 如果你的应用需要在后台使用蓝牙，需要检查以下权限（针对Android 10及以上版本）
        val hasBackgroundLocationPermission = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            ContextCompat.checkSelfPermission(
                context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED
        } else {
            true // Android 10以下不需要后台位置权限
        }
        return hasBluetoothPermission && hasBluetoothAdminPermission && hasBackgroundLocationPermission
    }

    fun applyBluetoothPermissions(context: Activity) {
        // 申请蓝牙权限
        val permissions = arrayOf(
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_BACKGROUND_LOCATION
        )
        val requestCode = 100
        // 申请权限
         ActivityCompat.requestPermissions(context, permissions, requestCode)
    }

}